package edu.task.controller;

import edu.task.common.base.BaseController;
import edu.task.common.result.RestResult;
import edu.task.model.SysMsg;
import edu.task.model.vo.SysExplainVO;
import edu.task.model.vo.SysMsgVO;
import edu.task.model.vo.SysTaskVO;
import edu.task.model.vo.SysUserVO;
import edu.task.service.SysMenuService;
import edu.task.service.SysMsgService;
import edu.task.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/system/msg")
public class SysMsgController extends BaseController {
    @Autowired
    SysMsgService msgService;

    /**
     * 显示消息表所有数据
     */
    @GetMapping("/list")
    public RestResult list() {
        RestResult ajax = RestResult.success();
        ajax.put(RestResult.DATA_TAG, msgService.selectMsgList());
        return ajax;
    }

    /**
     * 修改更新
     */
    @PutMapping
    public RestResult edit(@Validated @RequestBody SysMsgVO msg) {
        boolean flag = msgService.updateMsg(msg);
        return toAjax(flag ? 1 : 0);
    }

    /**
     * 删除消息
     */
    @DeleteMapping("/{msgId}")
    public RestResult remove(@PathVariable("msgId") Long msgId){
        return toAjax(msgService.deleteMsgById(msgId));
    }

    /**
     * 删除消息数组
     */
    @DeleteMapping("/delete/{msgIds}")
    public RestResult remove3(@PathVariable Long[] msgIds){
        return toAjax(msgService.deleteMsgByIds(msgIds));
    }

    /**
     * 删除消息数组
     */
    @DeleteMapping("/delete")
    public RestResult remove2(@RequestBody SysMsgVO sysMsg){
        Long[] msgIds = sysMsg.getMsgIds();
        return toAjax(msgService.deleteMsgByIds(msgIds));
    }



    /**
     * 根据消息编号获取消息列表信息
     */
    @GetMapping(value = {"/","/{msgId}"})
    public RestResult getInfo(@PathVariable(value = "msgId",required = false) Long msgId){
        RestResult ajax = RestResult.success();
        if (StringUtils.isNotNull(msgId)){
            ajax.put(RestResult.DATA_TAG, msgService.selectMsgById(msgId));
        }
        return ajax;
    }

    /**
     * 根据用户编号获取消息列表信息
     */
    @GetMapping("/user/{userId}")
    public RestResult userList(@PathVariable(value = "userId",required = false) Long userId){
        RestResult ajax = RestResult.success();
        if (StringUtils.isNotNull(userId)){
            ajax.put(RestResult.DATA_TAG, msgService.selectMsgByUserId(userId));
        }
        return ajax;
    }



}
